package Realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.Realm;

public class MyRealm3 implements Realm {

	/**
	 * 
	 */
	public String getName() 
	{
		return "myRealm3";
	}
	
	/**
	 * 判断该Realm是否支持该Token
	 * 
	 * 仅支持UsernamePasswordToken
	 * 
	 */
	public boolean supports(AuthenticationToken token) 
	{
		return token instanceof UsernamePasswordToken;
	}
	
	/**
	 * 根据token进行验证，获取验证信息
	 */
	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token)
			throws AuthenticationException 
	{
		//System.out.println("进人MyRealm1进行验证1");
		
		String userName = (String) token.getPrincipal();//从token获取用户名
		String passwork = new String((char[])token.getCredentials());//从token中获取密码
		
		//System.out.println("进人MyRealm1进行验证2");
		
		//核心---验证过程
		if(!"zhang".equals(userName))
		{
			throw new UnknownAccountException("用户名错误");
		}
		if(!"123".equals(passwork))
		{
			throw new IncorrectCredentialsException("密码错误");
		}
		
		//如果验证成功,就返回一个 AuthenticationInfo 实现
		return new SimpleAuthenticationInfo("zhang@163.com",passwork,this.getName());
	}
}
